package com.cci.kangdao.common.push;

import com.cci.kangdao.common.push.entity.gettui.GeTui;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.TransmissionTemplate;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @description:
 * @author: joe
 * @company: CTY
 * @since: 2022/4/7 12:38
 * @version: V1.0
 **/
public class GeTuiConfig implements PushService {

    private static GeTui geTui = new GeTui();

    static {
        init();
    }

    private static void init() {
        Properties properties = new Properties();
        String type = "";
        try {
            InputStream inputStream = GeTuiConfig.class.getClassLoader().getResourceAsStream("projectconfig.properties");
            properties.load(inputStream);
            inputStream.close();
            geTui.setAppID(properties.getProperty("getui.appID"));
            geTui.setAppKey(properties.getProperty("getui.appKey"));
            geTui.setMasterSecret(properties.getProperty("getui.masterSecret"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    /**
     * 透传消息体
     *
     * @param transmissionContent 透传内容，不支持转义字符
     * @param transmissionType    收到消息是否立即启动应用： 1为立即启动，2则广播等待客户端自启动
     * @param content             通知消息体
     * @return
     */
    public TransmissionTemplate getTemplate(String transmissionContent, int transmissionType, String content) {
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(geTui.getAppID());
        template.setAppkey(geTui.getAppKey());
        template.setTransmissionContent(transmissionContent);
        template.setTransmissionType(transmissionType);
        APNPayload payload = new APNPayload();
        //在已有数字基础上加1显示，设置为-1时，在已有数字上减1显示，设置为数字时，显示指定数字
        payload.setAutoBadge("+1");
        payload.setContentAvailable(1);
        payload.setSound("default");
        //简单模式APNPayload.SimpleMsg
        payload.setAlertMsg(new APNPayload.SimpleAlertMsg(content));
        template.setAPNInfo(payload);
        return template;
    }

    /**
     * 消息单个推送
     *
     * @param cId                 cId
     * @param title               通知标题
     * @param content             通知内容
     * @param transmissionType    收到消息是否立即启动应用： 1为立即启动，2则广播等待客户端自启动
     * @param transmissionContent 透传内容，不支持转义字符
     */
    @Override
    public boolean push2Single(String cId, String title, String content, int transmissionType, String transmissionContent) {
        IGtPush push = new IGtPush(geTui.getAppKey(), geTui.getMasterSecret(), true);
        TransmissionTemplate template = getTemplate(transmissionContent, transmissionType, content);
        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        // 离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
        message.setPushNetWorkType(0);
        Target target = new Target();
        target.setAppId(geTui.getAppID());
        target.setClientId(cId);
        IPushResult ret = null;
        //测试时先注释了   等正式上线时放开
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        if (ret != null) {
            System.out.println(ret.getResponse().toString());
            if (ret.getResponse() != null) {
                Object result = ret.getResponse().get("result");
                if (result != null && "ok".equals(result.toString())) {
                    return true;
                }
            }
        } else {
            System.out.println("服务器响应异常");
        }
        return false;
    }

    /**
     * @param cId
     * @param content             通知消息体
     * @param transmissionContent 透传内容
     */
    @Override
    public boolean push2Single(String cId, String content, String transmissionContent) {
        return push2Single(cId, null, content, 2, transmissionContent);
    }

    @Override
    public IPushResult pushMessageToApp(String content, int transmissionType, String transmissionContent) {
        IGtPush push = new IGtPush(geTui.getAppKey(), geTui.getMasterSecret(), true);
        TransmissionTemplate template = getTemplate(transmissionContent, transmissionType, content);
        AppMessage message = new AppMessage();
        message.setOffline(true);
        message.setOfflineExpireTime(24 * 1000 * 3600);
        message.setData(template);
        // 推送给App的目标用户需要满足的条件
        List<String> appIdList = new ArrayList<String>();
        appIdList.add(geTui.getAppID());
        message.setAppIdList(appIdList);
        IPushResult result = push.pushMessageToApp(message);
        return result;
    }

}
